package com.hkx.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkx.entity.Admin;
import com.hkx.service.AdminService;
import com.hkx.vo.ResultVo;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author huangkexing
 * @since 2024-08-05
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    PasswordEncoder passwordEncoder;
    @GetMapping("/list")
    ResultVo<IPage<Admin>> list(Integer pageNo, Integer pageSize) throws InterruptedException {
        return  ResultVo.success(adminService.page(new Page<>(pageNo, pageSize))) ;
    }
    @PostMapping("/add")
    ResultVo<Boolean> add(String name, String nickyName, Integer sex, LocalDate birthday, String phone, String email, String password, MultipartFile file) throws InterruptedException, ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVo.success(adminService.add(name, nickyName, sex, birthday, phone, email, password, file), "添加管理员成功") ;
    }
    @GetMapping("/check")
    ResultVo<Boolean> check(Long id, String field, String value) {
        return ResultVo.success(adminService.check(id, field,value));
    }
    @GetMapping("/getById")
    ResultVo<Admin> getById(Long id) {
        return ResultVo.success(adminService.getById(id));
    }
    @PostMapping("/update")
    ResultVo<Boolean> update(Long id, String name, String nickyName, Integer sex, LocalDate birthday, String phone, String email, String icon) {
        return ResultVo.success(adminService.update(id, name, nickyName, sex, birthday, phone, email, icon), "修改管理员成功");
    }
    @PostMapping("/del")
    ResultVo<Boolean> del(Long id, Boolean status) {
        return ResultVo.success(adminService.del(id, status), status ? "启用管理员成功" : "禁用管理员成功");
    }

}
